package com.rslai.base.tool.servertest.command;

import com.rslai.base.tool.servertest.context.Context;
import com.rslai.base.tool.servertest.model.KeyValueStore;
import com.rslai.base.tool.servertest.response.Response;
import com.rslai.base.tool.servertest.transport.command.ExecuteCommand;
import com.rslai.base.tool.servertest.util.CloneUtil;

import java.util.*;
import java.util.Map.Entry;

import org.slf4j.Logger;

public class CallStepCommand extends ParameterizedCommand {
    ExecuteCommand command;

    public CallStepCommand(ExecuteCommand command, List<KeyValueStore> params) {
        super(params);
        this.command = command;
    }

    public String serviceId() {
        return this.command.getId();
    }

    protected Response doExecuteInternal(Response preResult, List<KeyValueStore> processedParams, Context context) {
        this.logger.info("call command<{}> is staring ...", this.command);
        return this.command.execute(processedParams);
    }

    public StepCommand doClone() {
        return new CallStepCommand(this.command, CloneUtil.cloneKeyValueStore(getParams()));
    }

    public Map<String, Object> toReport() {
        Map details = new HashMap();
        details.put("stepName", "执行:");
        details.put("name", this.command.toReport());
        details.put("params", getReportParameter());
        return details;
    }

    private List getReportParameter() {
        if (this.params == null) {
            return Collections.emptyList();
        }
        List params = new ArrayList();
        for (Iterator i$ = this.params.iterator(); i$.hasNext(); ) {
            KeyValueStore kvs = (KeyValueStore) i$.next();
            Object value = kvs.getValue();
            if (value instanceof Map) {
                Iterator i = ((Map) value).entrySet().iterator();
                while (i.hasNext()) {
                    java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
                    params.add(new KeyValueStore((String) entry.getKey(), entry.getValue()));
                }
            } else {
                params.add(new KeyValueStore(kvs.getName(), kvs.getValue()));
            }
        }
        return params;
    }

}
